package com.lagou.edu.wwyan.web1.minicat;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Mapper {
    private List<MappedHost> hosts = new ArrayList<>();
    private MappedHost defaultHost = null;

    public void addHost(String name , Host host){
        MappedHost mappedHost = new MappedHost(name, host);
        hosts.add(mappedHost);
        if (defaultHost == null){
            defaultHost = mappedHost;
        }
    }
    public void addContext(String hostName,Context context){
        MappedHost host = findHost(hostName);
        MappedContext mappedContext = new MappedContext(context.getPath(), context);
        host.addContext(mappedContext);

        for (Wrapper wrapper : context.getWrappers()) {
            mappedContext.addWrapper(new MappedWrapper(wrapper.getUri(),wrapper));
        }
    }

    public MappedHost findHost(String name){
        for (MappedHost host : hosts) {
            if (Objects.equals(name,host.name)){
                return host;
            }
        }
        return defaultHost;
    }

    /**
     * 获取处理请求的容器
     * @param hostName
     * @param url
     * @param mappingData
     */
    public void map(String hostName,String url,MappingData mappingData){
        MappedHost mappedHost = findHost(hostName);
        mappingData.setHost(mappedHost.object);
        //访问到web应用的路径
        String contextPath = "";
        if (!"/".equals(url)){
            String[] split = url.split("/");
            contextPath = url.startsWith("/") && url.length() > 1 ?   split[1] : split[0];
        }

        MappedContext mappedContext = null;
        for (MappedContext context : mappedHost.contexts) {
            if (Objects.equals(context.name,contextPath)){
                mappedContext = context;

                break;
            }
        }
        if (mappedContext != null){
            mappingData.setContext(mappedContext.object);
            //访问指定servlet的路径
            String uri = url.replaceFirst("/"+contextPath,"");
            for (MappedWrapper wrapper : mappedContext.wrappers) {
                if (Objects.equals(wrapper.name,uri)){
                    mappingData.setWrapper(wrapper.object);
                }
            }
        }


    }



    protected static class MappedElement<T>{
        public String name;
        public T object;

        public MappedElement(String name, T object) {
            this.name = name;
            this.object = object;
        }

    }

    protected  static  class MappedHost extends MappedElement<Host>{
        public List<MappedContext> contexts = new ArrayList<>();


        public MappedHost(String name, Host host) {
            super(name, host);
        }

        public void addContext(MappedContext context){
            contexts.add(context);
        }



    }

    protected  static  class MappedContext extends MappedElement<Context>{
        private List<MappedWrapper> wrappers = new ArrayList<>();
        public MappedContext(String name, Context context) {
            super(name, context);
        }
        public void addWrapper(MappedWrapper wrapper){
            wrappers.add(wrapper);
        }
    }

    protected static class MappedWrapper extends MappedElement<Wrapper>{
        public MappedWrapper(String name, Wrapper wrapper) {
            super(name, wrapper);
        }
    }


}
